<?php
namespace api\modules\v1\controllers;

use api\modules\v1\models\address\ConfigAddress;
use api\modules\v1\models\address\UserAddress;
use yii\helpers\ArrayHelper;
use yii\filters\auth\HttpHeaderAuth;
use common\models\Functions;
use Yii;
/**
 * Default controller for the `v1` module
 */
class AddressController extends DefaultController
{

    public $modelClass = 'api\modules\v1\models\user\UserAddress';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [                     //需要过滤掉不用token参数的方法
                    'address'
                ]
            ]
        ]);
    }

    //========================== 地址配置 ==============================

    public function actionAddress() {
        if (Yii::$app->request->isGet) {
            return (new ConfigAddress())->getAllAddress();
        }
    }

    //========================== 用户地址 ==============================

    /**
     * 获取地址列表
     * @return mixed
     */
    public function actionList() {
        if (Yii::$app->request->isGet) {
            return (new UserAddress())->getList();
        }
    }

    public function actionInfo($id = 0) {
        if (Yii::$app->request->isPost) {
            $model = new UserAddress();
            $model->setScenario('addEdit');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->addEdit();
            }
        }

        if (Yii::$app->request->isGet) {
            return (new UserAddress())->getInfo($id);
        }

        if (Yii::$app->request->isDelete) {
            return (new UserAddress())->delInfo($id);
        }
    }


    public function actionDefault() {
        if (Yii::$app->request->isGet) {
            return (new UserAddress())->getDefault();
        }
        if (Yii::$app->request->isPost) {
            return (new UserAddress())->saveDefault();
        }
    }
}
